<?php
/*
 * Project Name	: 打酱油的短链接生成器
 * Project Date	: 2014-01-01
 * File Name	: dbConnect.class.php
 * File Create	: 2014-01-02
 * Last Update	: 2014-01-02
 * Author 		: 海蓝
 */
/**************************************************************
 *                                                            *
 * This file is part of SkywindShortLink.                     *
 *                                                            *
 * SkywindShortLink is free project; you can redistribute it  *
 * and/or modify it under the terms of the GNU General Public *
 * License as published by the Free Software Foundation;      *
 * either version 3 of the License, or any later version.     * 
 *                                                            *
 **************************************************************/

class dbConnect{
	
	private $con;

/*****EDIT IT FOLLOW YOUR DATABASE SETTING*****/
	private $db_addr 	= "127.0.0.1:3306";
	private $db_user	= "root";
	private $db_pass	= "";

	private $dbName		= "shortlink";
	private $tableName 	= "linktable";
/******************EDIT END********************/

	public function __construct(){
		if(!($this->con = mysql_connect($this->db_addr, $this->db_user, $this->db_pass))){
			//die(mysql_error());
		}
		mysql_set_charset('utf8', $this -> con);
		mysql_select_db($this->dbName);
		//die("0");
	}

	public function getOneByUrl($url){
		$sql = "SELECT * FROM $this->tableName WHERE url='$url'";
		$raw = mysql_query($sql);
		$res = mysql_fetch_array($raw);
		return $res;
	}

	public function insertOne($sUrl , $url){
		$sql = "INSERT INTO $this->tableName(sUrl, url, genDate) VALUES('$sUrl', '$url', now())";
		mysql_query($sql);
	}
	public function existUrl($sUrl){
		$sql = "SELECT count(*) FROM $this->tableName WHERE sUrl='$sUrl'";
		$raw = mysql_query($sql);
		$res = mysql_fetch_array($raw);
		//var_dump($res);
		//die("cut");
		return $res[0];
	}
	public function getOneByShortUrl($sUrl){
		$sql = "UPDATE $this->tableName SET count = count+1, lastClick=now() WHERE sUrl='$sUrl'";
		mysql_query($sql);
		$sql = "SELECT * FROM $this->tableName WHERE sUrl='$sUrl'";
		$raw = mysql_query($sql);
		$res = mysql_fetch_array($raw);
		return $res;
	}
};
?>